package com.wedding.study;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import org.junit.Test;

import java.io.*;
import java.util.*;

public class Json2xml {
    public static void main(String[] args) throws Exception {
        Document doc = DocumentHelper.createDocument();
        JSONObject o = diguiss();
        String str = JSON.toJSONString(o, SerializerFeature.WriteNullListAsEmpty,
                SerializerFeature.DisableCircularReferenceDetect,
                SerializerFeature.PrettyFormat);
        System.out.println(str);
        JSONObject jsonObject = JSON.parseObject(str);
        Element root = doc.addElement("root");

        diguiobj(jsonObject, root);

        OutputFormat format = OutputFormat.createPrettyPrint();
        XMLWriter os = new XMLWriter(new FileWriter(new
                File("d:/createXml04.xml")), format);
        os.write(doc);
        // [8] 关闭资源
        os.flush();
        os.close();
    }

    // 数组中有数组,
    public static void diguiobj(JSONObject obj, Element parent){
        Set<String> set = obj.keySet();
        for (String key: set) {
            Element e = parent.addElement(key);
            Object o = obj.get(key);
            if(o instanceof JSONObject){
                diguiobj((JSONObject) o, e);
            }else if(o instanceof JSONArray){
                diguiarr((JSONArray) o,0, e);
            }else{
                e.addText(o+"");
            }
        }
    }
    public static void diguiarr(JSONArray arr, int arrindex, Element parent){
        for (int i = 0; i < arr.size(); i++) {
            Object o = arr.get(i);
            if(o instanceof JSONArray){
                Element element1 = parent.addElement(i + "");
                diguiarr((JSONArray) o, i, element1);
            }else if(o instanceof JSONObject){
                Element element1 = parent.addElement(i + "");
                diguiobj((JSONObject) o, element1);
            }else{
                Element e = parent.addElement(i + "");
                e.addText(o+"");
            }
        }
    }
    @Test
    public void erwere(){
        JSONObject o = diguiss();
        String str = JSON.toJSONString(o, SerializerFeature.WriteNullListAsEmpty, SerializerFeature.DisableCircularReferenceDetect);
        System.out.println(str);
    }
    public static JSONObject diguiss(){
        JSONObject oo = new JSONObject();
        Random r = new Random();
        for (int k = 0; k < 5; k++) {
            int i = r.nextInt(list.size());
            Object v = geto();
            oo.put(strs[i], v);
            if(v instanceof JSONObject){
                Object v2 = geto();
                JSONObject vv = (JSONObject) v;i = r.nextInt(list.size());
                vv.put(strs[i], v2);
                if(v2 instanceof JSONObject){
                    Object v3 = geto();
                    JSONObject vv2 = (JSONObject) v2;i = r.nextInt(list.size());
                    vv2.put(strs[i], v3);
                    if(v3 instanceof JSONObject){
                        Object v4 = geto();
                        JSONObject vv3 = (JSONObject) v3;i = r.nextInt(list.size());
                        vv3.put(strs[i], v4);
                        if(v4 instanceof JSONObject){
                            Object v5 = geto();
                            JSONObject vv4 = (JSONObject) v4;i = r.nextInt(list.size());
                            vv4.put(strs[i], v5);
                        }
                    }
                }
            }
        }
        return oo;
    }

    public static List list = new ArrayList();
    public static String [] strs = { "曹操","川建国", "@@@","汪峰","www","空"};
    static{
        JSONObject o = new JSONObject();
        o.put("测试","000000");
        JSONArray arr = new JSONArray();
        arr.set(0,"英雄");
        list.add("狗屎");
        list.add(arr);
        list.add(o);
        list.add(33);
        list.add(o);
        list.add(arr);
    }
    public static Object geto(){
        Random r = new Random();
        int i = r.nextInt(list.size());
        Object v = list.get(i);
        return v;
    }
}
